//
//  @ Project : Adamantium Project
//  @ File Name : LRMap.cpp
//  @ Date : 12/3/2012
//  @ Author : 
//
//


#include "LRMap.h"

LRMap::LRMap() {
	m_currentPos = 0;
}

LRMap::~LRMap()
{
	m_core = NULL;
	//m_LRMap.~GameTexture();
}

void LRMap::draw() {
	m_LRMap.draw();//draws texture

	//horizontal top to bottom
	m_core->drawLine( m_gridlineTopLeft, m_gridlineTopRight,GRIDLINES_COLOUR);
	m_core->drawLine( m_HGridlineLeft1, m_HGridlineRight1,GRIDLINES_COLOUR);
	m_core->drawLine( m_HGridlineLeft2, m_HGridlineRight2,GRIDLINES_COLOUR);
	m_core->drawLine( m_gridlineBottomLeft, m_gridlineBottomRight, GRIDLINES_COLOUR);
	//vertical line left to right
	m_core->drawLine( m_gridlineTopLeft, m_gridlineBottomLeft,GRIDLINES_COLOUR);
	m_core->drawLine( m_VGridlineTop1, m_VGridlineBottom1,GRIDLINES_COLOUR);
	m_core->drawLine( m_VGridlineTop2, m_VGridlineBottom2,GRIDLINES_COLOUR);
	m_core->drawLine( m_gridlineTopRight, m_gridlineBottomRight,GRIDLINES_COLOUR);

	//then draw darkspace image or numbers in each box
	for(int i = 0; i < MAX_LRSIZE; ++i) {
		if(m_mapData[i]->isDarkSpace) {
			m_DarkSpace.m_pos = m_gridDrawPos[i];
			m_DarkSpace.update();
			m_DarkSpace.draw();
		}
		else if(m_mapData[i]->superNova) {
			V2DF gridOffset = V2DF(m_gridlineTopLeft.x + (GRID_PIXEL_SIZE*0.5f) + (i%3 * GRID_PIXEL_SIZE),
				m_gridlineTopLeft.y + (GRID_PIXEL_SIZE*0.5f) + (int(i/3) * GRID_PIXEL_SIZE) );
			m_pSupernovae->m_pos = gridOffset;
			m_pSupernovae->update();
			m_pSupernovae->draw();
		}
		else{
			char temp[10];
			sprintf(temp, "%d%d%d", 0/*starbases*/, 
				m_mapData[i]->enemyNumbers()/*enemies*/,
				m_mapData[i]->starNumbers()/*stars*/);
			m_core->drawText( temp, m_gridDrawPos[i], 0xFFFFFFFF);
		}
	}
}

void LRMap::init(TemplateVector<SRMap*> a_data, BeanieCore * a_core) {
	m_core = a_core;
	//then set data coming in from galaxy map which will be a 3x3 grid. top left to bottom right
	m_mapData.copy(a_data);

	//set background image for mini map
	backgroundUScale = 0.9375f;
	m_backgroundOffset.x = 140;
	m_backgroundOffset.y = 540;
	m_LRMap.initTexture(a_core, "sprites/starsBackground1LRMap.png", TEX_CENTER,
		m_backgroundOffset, V2DF( 1, 1), 0.0f);
	m_DarkSpace.initTexture(a_core, "sprites/DarkSpace.png", TEX_CENTER,
		V2DF(0,0), V2DF(1,1), 0.0f);

	m_LRMap.update();
	m_LRMap.m_scl.x = backgroundUScale;
	m_LRMap.m_scl.y = backgroundUScale;
	m_LRMap.update();
	//define grid lines by the background texture scale and offset
	m_gridlineTopLeft.x = m_backgroundOffset.x - ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineTopLeft.y = m_backgroundOffset.y - ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineTopRight.x = m_backgroundOffset.x + ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineTopRight.y = m_backgroundOffset.y - ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineBottomLeft.x = m_backgroundOffset.x - ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineBottomLeft.y = m_backgroundOffset.y + ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineBottomRight.x = m_backgroundOffset.x + ( (256/*size of texture*/*backgroundUScale)/2);
	m_gridlineBottomRight.y = m_backgroundOffset.y + ( (256/*size of texture*/*backgroundUScale)/2);
	//set horizontal line points
	m_HGridlineLeft1.x = m_HGridlineLeft2.x = m_gridlineTopLeft.x;
	m_HGridlineRight1.x = m_HGridlineRight2.x = m_gridlineTopRight.x;
	m_HGridlineLeft1.y = m_gridlineTopLeft.y + 80;//80 pixel margin
	m_HGridlineLeft2.y = m_gridlineTopLeft.y + 160;//80 pixel margin from previous
	m_HGridlineRight1.y = m_HGridlineLeft1.y;
	m_HGridlineRight2.y = m_HGridlineLeft2.y;//making them on same pixel plane
	//set vertical line points
	m_VGridlineTop1.y = m_VGridlineTop2.y = m_gridlineTopLeft.y;
	m_VGridlineBottom1.y = m_VGridlineBottom2.y = m_gridlineBottomLeft.y;
	m_VGridlineTop1.x = m_gridlineTopLeft.x + 80;//80 pixel margin
	m_VGridlineTop2.x = m_gridlineTopLeft.x + 160;//80 pixel margin from previous
	m_VGridlineBottom1.x = m_VGridlineTop1.x;
	m_VGridlineBottom2.x = m_VGridlineTop2.x;//making them on same pixel plane

	//set grid draw positions for drawing text or images onto lrmap
	m_gridDrawPos[0].x = m_backgroundOffset.x - GRID_PIXEL_SIZE;	m_gridDrawPos[0].y = m_backgroundOffset.y - GRID_PIXEL_SIZE;
	m_gridDrawPos[1].x = m_backgroundOffset.x;						m_gridDrawPos[1].y = m_backgroundOffset.y - GRID_PIXEL_SIZE;
	m_gridDrawPos[2].x = m_backgroundOffset.x + GRID_PIXEL_SIZE;	m_gridDrawPos[2].y = m_backgroundOffset.y - GRID_PIXEL_SIZE;
	m_gridDrawPos[3].x = m_backgroundOffset.x - GRID_PIXEL_SIZE;	m_gridDrawPos[3].y = m_backgroundOffset.y;
	m_gridDrawPos[4].x = m_backgroundOffset.x;						m_gridDrawPos[4].y = m_backgroundOffset.y;
	m_gridDrawPos[5].x = m_backgroundOffset.x + GRID_PIXEL_SIZE;	m_gridDrawPos[5].y = m_backgroundOffset.y;
	m_gridDrawPos[6].x = m_backgroundOffset.x - GRID_PIXEL_SIZE;	m_gridDrawPos[6].y = m_backgroundOffset.y + GRID_PIXEL_SIZE;
	m_gridDrawPos[7].x = m_backgroundOffset.x;						m_gridDrawPos[7].y = m_backgroundOffset.y + GRID_PIXEL_SIZE;
	m_gridDrawPos[8].x = m_backgroundOffset.x + GRID_PIXEL_SIZE;	m_gridDrawPos[8].y = m_backgroundOffset.y + GRID_PIXEL_SIZE;

}

void LRMap::reInit(TemplateVector<SRMap*> a_data) {
	m_mapData.copy(a_data);
}

void LRMap::GrabBoundingBox(Clickable &a_box)
{
	a_box.TOP = m_gridlineTopLeft.y;
	a_box.LEFT = m_gridlineTopLeft.x;
	a_box.DOWN = m_gridlineBottomRight.y;
	a_box.RIGHT = m_gridlineBottomRight.x;
}